﻿using IShoppingBll;
using IShoppingDal;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static ShoppingEntity.EvaluateInfo;

namespace ShoppingBll
{
    public class EvaluateInfoBll : BaseBll<EvaluateInfo>, IEvaluateInfoBll
    {
        private IEvaluateInfoDal _evaluateInfoDal;
        private IOrderInfoDal _orderInfoDal;
        private ICommodityInfoDal _commodityInfoDal;
        private IUserInfoDal _userInfoDal;


        public EvaluateInfoBll(IEvaluateInfoDal evaluateInfoDal, IUserInfoDal userInfoDal,IOrderInfoDal orderInfoDal, ICommodityInfoDal commodityInfoDal) : base(evaluateInfoDal)
        {
            _evaluateInfoDal = evaluateInfoDal;
            _orderInfoDal = orderInfoDal;
            _commodityInfoDal = commodityInfoDal;
            _userInfoDal = userInfoDal;
        }

        public bool AddEvaluate(string orderId, string content, EvaluateStateEnum state)
        {
            OrderInfo orderInfo = _orderInfoDal.GetEntity(orderId);
            EvaluateInfo evaluateInfo = new EvaluateInfo()
            {
                ID = Guid.NewGuid().ToString(),
                GoodsId = orderInfo.GoodsId,
                UserId = orderInfo.UserId,
                Content = content,
                CreateTime = DateTime.Now,           
                State=state              
            };
            bool isSuccess = _evaluateInfoDal.Add(evaluateInfo);
            if (isSuccess)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 分页模糊查询、
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <param name="commodityName"></param>
        /// <returns></returns>
        public object GetEvaluateInfoList(int page, int limit, out int count, string commodityName)
        {
            var Temp = from e in _evaluateInfoDal.GetEntitiesDb()
                       join c in _commodityInfoDal.GetEntitiesDb()
                       on e.GoodsId equals c.ID into ecTemp
                       from ec in ecTemp.DefaultIfEmpty()

                       join u in _userInfoDal.GetEntitiesDb()
                       on e.UserId equals u.ID into euTemp
                       from eu in euTemp.DefaultIfEmpty()
                       select new
                       {
                           e.ID,
                           ec.CommodityName,
                           e.State,
                           eu.UserName,
                           e.CreateTime,
                           e.Content
                       };

            if (!string.IsNullOrEmpty(commodityName))
            {
                Temp = Temp.Where(t => t.CommodityName.Contains(commodityName));
            }

            //统计
            count = Temp.Count();

            //分页
            Temp = Temp.OrderBy(s => s.CreateTime).Skip((page - 1) * limit).Take(limit);


            var list = Temp.ToList().Select(c => 
            {
                string UserName;
                string State = c.State.ToString();
                if (State == "匿名")
                {
                    UserName = "匿名用户";
                }
                else
                {
                    UserName = c.UserName;
                }
                return new
                {
                    c.ID,
                    c.CommodityName,
                    c.Content,
                    UserName,
                    CreateTime=c.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
                };

            });

            return list;
        }




    }
}
